package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.ClassifyGiftActivity;
import com.ailiwu.www.ailiwu.bean.ClassifyGift;
import com.ailiwu.www.ailiwu.url.Urls;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzw on 2015/12/5.
 */
public class GiftGridRecycAdapter extends RecyclerView.Adapter<GiftGridRecycAdapter.ViewHolder>{
    private List<ClassifyGift.DataEntity.CategoriesEntity.SubcategoriesEntity> mSubcategories;
    private LayoutInflater mInflater;
    private static Context mContext;
    private BitmapUtils bitType;


    public GiftGridRecycAdapter(Context context, Object datas){
        mInflater = LayoutInflater.from(context);
        mContext = context;

        mSubcategories = new ArrayList<>();
        if (datas != null) {
            mSubcategories = (List<ClassifyGift.DataEntity.CategoriesEntity.SubcategoriesEntity>) datas;
            bitType = new BitmapUtils(mContext);
//			bitType.display(iv, "http://bbs.lidroid.com/static/image/common/logo.png");
            //设置是否支持本地缓存
            bitType.configDiskCacheEnabled(true);
            //设置是否支持内存缓存
            bitType.configMemoryCacheEnabled(true);
            //设置图片的最大宽高
            bitType.configDefaultBitmapMaxSize(60, 60);
            //设置图片的默认颜色处理方式
            bitType.configDefaultBitmapConfig(Bitmap.Config.ARGB_8888);
            //设置缓存的有效期
            bitType.configDefaultCacheExpiry(24*3600*1000);
            //设置联网读取图片的超时时间
            bitType.configDefaultConnectTimeout(20000);
            //设置图片的默认BitmapDisplayConfig对象
//			bitType.configDefaultDisplayConfig(displayConfig)
            //设置本地图片读取的超时时间
//			bitType.configDefaultReadTimeout(readTimeout);
            //设置加载失败显示的图片
//			bitType.configDefaultLoadFailedImage(bitmap);
            //设置加载过程中显示的图片
//			bitType.configDefaultLoadingImage(bitmap);
            //设置连接线程数
//			bitType.configThreadPoolSize(threadPoolSize);
        }

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_type_strategy, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvTitle.setText(mSubcategories.get(position).getName());

        //绑定显示的图片
        bitType.display(holder.ivIcon, mSubcategories.get(position).getIcon_url());

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ClassifyGiftActivity.class);
                intent.putExtra("urlID", Urls.giftRecycUrl(mSubcategories.get(position).getId()));
                intent.putExtra("name",mSubcategories.get(position).getName());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mSubcategories == null? 0:mSubcategories.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ImageView ivIcon;
        View view;
        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_type_startegy);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_type_startegy);
            this.view = itemView;


        }
    }
}
